package view
{
	import flash.events.Event;
	
	import model.DataProxy;
	import model.ViewProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class LoginViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String 				= "LoginViewMediator";
		
		private var loginView:LoginView;
		
		public function LoginViewMediator( viewComponent:Object = null )
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			loginView = new LoginView();
			loginView.addEventListener( LoginView.CLICKED, loginViewClickedHandler );
			loginView.init( viewProxy.vo );
			
			viewProxy.vo.loginView = loginView;
			loginView.x = Math.round( viewProxy.vo.HEADER_WIDTH );
			
			viewComponent.addChild( loginView );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ LoginView.SHOW, LoginView.WRONG_LOGIN, LoginView.SUCCESS_LOGIN ];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch( name )
			{
				case LoginView.SHOW:
						loginView.show();
					break;
				case LoginView.WRONG_LOGIN:
						loginView.unselect();
					break;
				case LoginView.SUCCESS_LOGIN:
					loginView.unselect();
					loginView.validateUser( body );
					break;
			}
		}
		
		private function loginViewClickedHandler( e:Event ):void
		{
			dataProxy.validateUser( e.target.info );
		}
		
		public function get dataProxy():DataProxy
		{
			return facade.retrieveProxy( DataProxy.NAME ) as DataProxy;
		}
		
		public function get viewProxy():ViewProxy
		{
			return facade.retrieveProxy( ViewProxy.NAME ) as ViewProxy;
		}
	}
}